package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbEnvironmentDao;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbEnvironmentEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbEnvironment;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbEnvironmentCustomWhere;
import com.ds.infrastructure.hyperspace.console.exceptions.ArgumentValidException;
import com.ds.infrastructure.hyperspace.console.service.inf.IEnvironmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: yaozhendong
 * @create: 2019-09-18 11:19
 **/

@Service
public class EnvironmentService implements IEnvironmentService {
    @Autowired
    TbEnvironmentDao tbEnvironmentDao;

    @Override
    public int save(TbEnvironment record) {
        return tbEnvironmentDao.insertSelective(record);
    }

    @Override
    public int update(TbEnvironment record) {
        return tbEnvironmentDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbEnvironment get(Long id) {
        TbEnvironment entity = tbEnvironmentDao.selectByPrimaryKey(id);
        if (entity != null && TbPublicEnum.GMD_IS_DEL.NO.code.equals(entity.getGmtIsDeleted())) {
            return entity;
        }
        return null;
    }

    @Override
    public int delete(Long id) {
        TbEnvironment item = new TbEnvironment();
        item.setId(id);
        item.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.YES.code);
        return update(item);
    }

    @Override
    public List<TbEnvironment> getList(TbEnvironment where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbEnvironmentDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbEnvironment where, TbCustomWhere customWhere) {
        return tbEnvironmentDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbEnvironment selectOne(TbEnvironment where, TbCustomWhere customWhere) {
        List<TbEnvironment> itemList = tbEnvironmentDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    /**
     * 检查并获得环境
     *
     * @param id
     * @return
     */
    @Override
    public TbEnvironment getOrThrow(Long id) {
        TbEnvironment tbEnvironment = get(id);
        if (tbEnvironment == null) {
            throw new ArgumentValidException("获取环境信息失败，没有找到对应的记录;当前id=" + id);
        }
        return tbEnvironment;
    }

    /**
     * 获得环境名称与ID的映射集合
     *
     * @param idList
     * @return
     */
    @Override
    public Map<Long, String> getMap(List<Long> idList) {

        TbEnvironmentCustomWhere customWhere = new TbEnvironmentCustomWhere().setIdList4In(idList);
        List<TbEnvironment> tbEnvironmentList = getList(null, customWhere, TbPageWhere.newPage(0, idList.size()));
        if (tbEnvironmentList.isEmpty()) {
            return Collections.emptyMap();
        }
        return tbEnvironmentList.stream().collect(Collectors.toMap(TbEnvironment::getId, TbEnvironment::getEnvironmentName));
    }

    @Override
    public TbEnvironment getAndSave(String environmentName) {
        TbEnvironment record = new TbEnvironment();
        record.setEnvironmentName(environmentName);
        record.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        TbEnvironment item4Select = selectOne(record, null);
        if (item4Select != null) {
            return item4Select;
        }
        save(record);
        return record;
    }

    @Override
    public Long fork(TbEnvironment entity) {
        TbEnvironment newEntity = new TbEnvironment();
        newEntity.setParentId(entity.getId());
        newEntity.setEnvironmentName(String.format("%s-fork", entity.getEnvironmentName()));
        newEntity.setPackageName(entity.getPackageName());
        newEntity.setUsageState(TbEnvironmentEnum.USAGE_STATE.YES.code);
        newEntity.setDescription(entity.getDescription());
        newEntity.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        save(newEntity);
        return newEntity.getId();
    }

    @Override
    public List<TbEnvironment> getAll() {
        TbEnvironment where = new TbEnvironment();
        where.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        return getList(where, null, TbPageWhere.newPage4All());

    }

}
